/**
 * 
 */
package com.yidin.lsc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yidin.entry.MyFriendListEntry;
import com.yidin.entry.PhotoListEntry;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;

import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;

/**
 * 我的好友页面
 * 
 * @author cola
 * 
 */
public class MyFriends extends Container implements OnCheckedChangeListener {
	protected Button listBtn;
	protected Button speedDialBtn;
	protected Button mapBtn;
	protected ViewGroup ViewContainer;
	protected List<View> group;
	protected Button rightBtn;
	private RadioGroup radioGroup;
	protected ListView myListView;
	protected List<MyFriendListEntry> netInfos = null;
	protected AddressListAdapter addressListAdapter;

	public MyFriends(Activity activity, View contentView, Views father) {
		super(activity, contentView, father);
		// TODO Auto-generated constructor stub
		initialize();
	}

	@Override
	public void checkCache() {
		// TODO Auto-generated method stub

	}

	@Override
	public void findView(View contentView) {
		// TODO Auto-generated method stub
		View view = null;
		view = inflater.inflate(R.layout.talk, null);
		group.add(view);

		ViewContainer.addView(view);

		this.listBtn = (Button) contentView.findViewById(R.id.list);
		this.listBtn.setText("会话");
		this.speedDialBtn = (Button) contentView.findViewById(R.id.speedDial);
		this.speedDialBtn.setText("好友");
		this.mapBtn = (Button) contentView.findViewById(R.id.map);
		this.mapBtn.setText("通讯录");
		this.rightBtn = (Button) contentView.findViewById(R.id.filter);
	}

	@Override
	public void showFunctionsListView(View contentView) {
		// TODO Auto-generated method stub
		this.group = new ArrayList<View>();
		ViewContainer = (ViewGroup) contentView
				.findViewById(R.id.ViewContainer);
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
//		this.netInfos = new ArrayList<AddressList>();
//		AddressList a1 = new AddressList();
//		a1.im = "一点事";
//		a1.name = "太好玩了";
//		this.netInfos.add(a1);
//		AddressList a2 = new AddressList();
//		a2.im = "大小圈真给力！！";
//		a2.name = "yidin.net";
//		this.netInfos.add(a2);
		this.myListView = (ListView) contentView
				.findViewById(R.id.myFriendListView);
//		addressListAdapter = new AddressListAdapter(activity, netInfos);
//		this.myListView.setAdapter(addressListAdapter);
		this.myListView.setOnItemClickListener(listOnItemClickListener);
		this.rightBtn.setOnClickListener(loseOnClickListener);

		Map obj = new HashMap<String, String>();
		obj.put("AccountId", "hlf");
		obj.put("FriendType", "-1");
		((LscActivity) activity).showProgress(activity);
		Task task = new Task(Task.TASK_USER_FRIENDSLIST, obj);
		MainService.AddNewTask(task);
	}

	public void removeView() {
		ViewContainer.removeAllViews();
	}

	private OnItemClickListener listOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, OtherFriendData.class);
			it.putExtra("AccountFrom", netInfos.get(position).AccountId);
			activity.startActivity(it);
			// Intent it = new Intent(activity,IM.class);
			// activity.startActivityForResult(it, 1);
		}

	};

	private OnClickListener loseOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			View view = inflater.inflate(R.layout.dynamicsetting, null);
			ViewContainer.getChildAt(0).setVisibility(View.GONE);
			if (ViewContainer.getChildCount() > 1)
				ViewContainer.removeViewAt(1);
			ViewContainer.addView(view);

			radioGroup = (RadioGroup) contentView.findViewById(R.id.radioGroup);
			radioGroup.setOnCheckedChangeListener(MyFriends.this);
			// contentView = father.addView(R.layout.dynamicsetting);
		}

	};

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		int task = (Integer) param[0];
		switch (task) {
		case Task.TASK_USER_FRIENDSLIST:
			netInfos = (List<MyFriendListEntry>) param[1];
			if (netInfos == null)
				return;
			addressListAdapter = new AddressListAdapter(activity, netInfos);
			this.myListView.setAdapter(addressListAdapter);
			break;
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch (checkedId) {
		case R.id.all: {
			break;
		}
		case R.id.my: {
			break;
		}
		case R.id.friend: {
			break;
		}
		}
		ViewContainer.removeViewAt(1);
		ViewContainer.getChildAt(0).setVisibility(View.VISIBLE);
		// int keyCode = KeyEvent.KEYCODE_BACK;
		// KeyEvent event1 = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);//
		// activity.onKeyDown(keyCode, event1);
		// activity.dispatchTouchEvent(event);
	}
}
